<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Windows Live Connect Poll</title>
    <style>
        <!--
        .Name
        {
            font-family: Segoe UI, Verdana, Tahoma, Helvetica, Arial, sans-serif;
            font-weight: bold;
        }
        -->
    </style>	
</head>
<body>
<h1>Windows Live Connect Poll</h1>
<div>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="ajax_framework.js" language="javascript"></script>
<form action="javascript:guardarEmail()" method="post">
	<input name="email" type="hidden" id="email"  >
	<input name="nombre" type="hidden" id="nombre"  >
	<input id="btnMas" type="hidden" name="Submit" />
</form>
<div id="insert_gracias" style="display:none;" > Gracias por votar (: </div>
<div class="infobox" id="infobox" >
	<p><div id="insert_response"><?php echo $resul; ?></div></p>
</div>
<div id="meName" class="Name"></div>
<div id="meImg"></div>
<div id="signin"></div>
</div>
<script src="//js.live.net/v5.0/wl.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
    function setMe(clear) {
        var imgHolder = document.getElementById("meImg"),
            meNameHolder = document.getElementById("meName");
        if (clear) {
            imgHolder.innerHTML = "";
            meNameHolder.innerHTML = "";
            meImgInitialized = false;
			javascript:cerrarPoll();//cerrar la encuesta cdo no esta logueado
			div = document.getElementById('infobox');
			div.style.display='none';//ocultar los mensajes de la BD
			javascript:ocultarGracias()
            return;
        }
        if (meImgInitialized) return;
        var session = WL.getSession(),
            token = session != null ? session.access_token : null;
        if (token != null) {
            var url = "https://apis.live.net/v5.0/me/picture?access_token=" + escape(token);
            imgTagString = "<img src='" + url + "' />";
            imgHolder.innerHTML = imgTagString;
            WL.api({ path: "me", method: "get" }, function (response) {
                if (!response.error) {
                   document.getElementById("meName").innerHTML = response.first_name + " " + response.last_name;
				   document.getElementById("email").value = response.emails.account;
				   document.getElementById("nombre").value = response.first_name + " " + response.last_name;
				   javascript:mostrarPoll();
                }
            });
            meImgInitialized = true;
        }
    }
    // Update the following values
    var client_id = "000000004C06AA1C",
        scope = ["wl.signin", "wl.basic", "wl.offline_access", "wl.emails"],
        redirect_uri = "http://despensadm.com.ar/labs/liveconnectpolls/callback.php";
    var meImgInitialized = false;
    WL.Event.subscribe("auth.login", function () {
        setMe(false);
    });
    WL.Event.subscribe("auth.logout", function () {
        setMe(true);
    });
    WL.init({ client_id: client_id, redirect_uri: redirect_uri, response_type: "code", scope: scope  });
    WL.ui({ name: "signin", element: "signin", scope: scope });
    setMe(false);
</script>


<script type="text/javascript" language="javascript">
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function mostrarPoll() {
div = document.getElementById('poll');
div.style.display = '';
}

function cerrarPoll() {
div = document.getElementById('poll');
div.style.display='none';
}

function mostrarGracias() {
div = document.getElementById('insert_gracias');
div.style.display = '';
}
function ocultarGracias() {
div = document.getElementById('insert_gracias');
div.style.display='none';
}

</script>
<div id="poll" style="display:none;" >
<h3>Elegir</h3>
<form name="pollForm" action="" method="post" >
<p><label for="opcion0"><input type="radio" value="opcion0" name="opcion" id="opcion0"> Opcion 0</label></p>
<p><label for="opcion1"><input type="radio" value="opcion1" name="opcion" id="opcion1"> Opcion 1</label></p>
<p><label for="opcion2"><input type="radio" value="opcion2" name="opcion" id="opcion2"> Opcion 2</label></p>
<p><label for="opcion3"><input type="radio" value="opcion3" name="opcion" id="opcion3"> Opcion 3</label></p>
<p><label for="opcion4"><input type="radio" value="opcion4" name="opcion" id="opcion4"> Opcion 4</label></p>

<input id="btnVotar" type="button" name="Submit" onclick="if(confirm('Ha votado la opcion: '+getCheckedValue(document.forms['pollForm'].elements['opcion'])+'?')){javascript:guardarEmail(getCheckedValue(document.forms['pollForm'].elements['opcion']));javascript:mostrarGracias();}" value="Votar" />
</form>
</div>

</body>
</html>